package com.grasp.activity.area;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.grasp.activity.R;
import com.grasp.base.ActionBar;
import com.grasp.base.ActionBarOnClick;
import com.grasp.base.BaseActivity;
import com.grasp.data.IconItem;
import com.grasp.db.AreaTable;
import com.grasp.util.HelperUtil;
import com.grasp.util.HelperUtil.OnClickIconItem;

/**
 * 添加区域 所需参数IconItemStr(IconItem)要修改的区域,当IconItem为null时为添加区域,有值时为修改区域
 */
public class AddAreaActivity extends BaseActivity {

	private ImageView iconImageView;
	private IconItem mIconItem;
	private AreaTable mAreaTable;
	private EditText areaNameEdit;
	private boolean isInsert = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.area_add_layout);
		mAreaTable = new AreaTable(this);
		iconImageView = (ImageView) findViewById(R.id.area_select_icon_view);
		areaNameEdit = (EditText) findViewById(R.id.area_add_name_view);
		initActionBar();
	}

	private void initActionBar() {
		ActionBar mActionBar = (ActionBar) findViewById(R.id.area_add_area_actionbar);
		mIconItem = (IconItem) getIntent().getSerializableExtra("IconItemStr");
		if (null == mIconItem) {
			isInsert = true;
			mIconItem = new IconItem();
			mActionBar.setTitle(R.string.area_add_str);
		} else {
			mActionBar.setTitle(R.string.area_update_str);
			int id = HelperUtil.findIdByName(this, mIconItem.getFileName());
			iconImageView.setImageResource(id);
			areaNameEdit.setText(mIconItem.getTitle());
			isInsert = false;
		}
		mActionBar.setLeftBtnText(R.string.action_back);
		mActionBar.setRightBtnBackground(R.drawable.actionbar_sure_btn_bg);
		mActionBar.setActionBarOnClickListener(new ActionBarOnClick() {

			@Override
			public void rightBtnOnClick() {
				String areaName = areaNameEdit.getText().toString();
				if (TextUtils.isEmpty(areaName)) {
					Toast.makeText(AddAreaActivity.this,
							R.string.un_input_name_str, Toast.LENGTH_SHORT)
							.show();
				} else if (TextUtils.isEmpty(mIconItem.getFileName())) {
					Toast.makeText(AddAreaActivity.this,
							R.string.un_select_iocn_str, Toast.LENGTH_SHORT)
							.show();
				} else {
					mIconItem.setTitle(areaName);
					if (isInsert)
						mAreaTable.insert(mIconItem);
					else {
						mIconItem.setTitle(areaName);
						mAreaTable.update(mIconItem);
					}
					finish();
				}
			}

			@Override
			public void leftBtnOnClick() {
				finish();
			}
		});
	}

	public void selectIconView(View view) {
		HelperUtil.initDialogUtil().selectIconDialog(this,
				new OnClickIconItem() {

					@Override
					public void selectIconItem(IconItem iconItem) {
						mIconItem.setFileName(iconItem.getFileName());
						int id = HelperUtil.findIdByName(AddAreaActivity.this,
								mIconItem.getFileName());
						iconImageView.setImageResource(id);
					}
				});
	}
}
